import { AxiosRequestConfig, AxiosResponse } from 'axios/index';
import { PresignedUrlsResponse, UploadIdResponse } from 'src/api/dataCenter';
import { authedApi } from 'src/utils/axios';
import {
  ActionFeatureSetReq,
  UploadFeatureSetList,
} from 'stores/platform-featureset-upload';
import { UploadList } from 'stores/platform-upload';

export interface BaseFeatureSetReq {
  project_id: string;
  feature_set_id: string;
}
interface FeatureSetReq extends BaseFeatureSetReq {
  upload_id: string;
}

export interface ImportFileReq extends BaseFeatureSetReq {
  data: {
    filename: string;
  };
}

interface createFeatureSetReq {
  title: string;
  description?: string;
}
interface CreateFeatureSetResponse {
  feature_set_id: string;
}

export async function createFeatureSet(
  project_id: string,
  params: createFeatureSetReq
) {
  return authedApi.post<any, AxiosResponse<CreateFeatureSetResponse>>(
    `v2/projects/${project_id}/feature_set/async_create/`,
    params
  );
}

interface GetUploadIdReq extends BaseFeatureSetReq {
  row_name: string;
  chunk_size: number;
  total_size: number;
  identifier: string;
}

export async function getUploadId(
  req: GetUploadIdReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post<any, AxiosResponse<UploadIdResponse>>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/upload_task/`,
    {
      row_name: req.row_name,
      chunk_size: req.chunk_size,
      total_size: req.total_size,
      identifier: req.identifier,
    },
    {
      ...config,
    }
  );
}

export async function getImportedFiles(req: BaseFeatureSetReq) {
  return authedApi.get<{ isImported: boolean }>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/imported_files/`
  );
}

export async function mergeChunks(
  req: FeatureSetReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/merge_chunks/`,
    { upload_id: req.upload_id },
    {
      ...config,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }
  );
}

interface uploadInfoResponse {
  ids: string[];
}

export async function getChunkUploadedInfo(req: FeatureSetReq) {
  return authedApi.post<any, AxiosResponse<uploadInfoResponse>>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/chunk_info/`,
    { upload_id: req.upload_id }
  );
}

export async function importFile(
  req: ImportFileReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/import_file/`,
    req.data,
    {
      ...config,
    }
  );
}

export async function getUploadList() {
  return authedApi.get<UploadFeatureSetList[]>('v2/upload_feature_set_list/');
}

export async function errorCleanUnUpload(feature_set_id: string) {
  return authedApi.patch(`v2/upload_feature_set_error/`, { feature_set_id });
}

export async function updateStatus(req: FeatureSetReq) {
  return authedApi.patch(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/update_status/`,
    { upload_id: req.upload_id }
  );
}

export async function getUnCompleteFileList(req: BaseFeatureSetReq) {
  return authedApi.get<UploadFeatureSetList[]>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/uploaded_lists/`
  );
}

export async function updateErrorStatus(req: ActionFeatureSetReq) {
  return authedApi.patch(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/update_fail_status/`,
    { action: req.action }
  );
}

export async function getPresignedUrls(
  req: FeatureSetReq,
  config?: AxiosRequestConfig
) {
  return authedApi.post<any, AxiosResponse<PresignedUrlsResponse>>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/presigned_urls/`,
    {
      upload_id: req.upload_id,
    },
    {
      ...config,
    }
  );
}

export async function getFileId(req: FeatureSetReq) {
  return authedApi.post<UploadList>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/file_id/`,
    { upload_id: req.upload_id }
  );
}

export async function updateImporting(req: BaseFeatureSetReq) {
  return authedApi.patch(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/update_importing/`
  );
}

export async function errorDeleteFUnUpload(feature_set_id: number) {
  return authedApi.patch(`v2/upload_feature_set_del/`, { feature_set_id });
}

interface UpdateItemStatus extends BaseFeatureSetReq {
  id: number;
  status: string;
}

export async function updateItemStatus(req: UpdateItemStatus) {
  return authedApi.post<UploadList[]>(
    `v3/projects/${req.project_id}/feature_set/${req.feature_set_id}/minio/${req.id}/update_item_status/`,
    {
      error_status: req.status,
    }
  );
}
